﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RobotCleaner
{
    /// <summary>
    /// Sep 24,2013 14:29 EndlessRain
    /// http://bbs.csdn.net/topics/330174822
    /// </summary>
    public partial class MyPanel : Panel
    {
        public MyPanel()
        {
            InitializeComponent();
            btp = new Bitmap(this.Width, this.Height); // 初始化缓冲数据对象
            grp = Graphics.FromImage(btp); // 初始化缓冲绘画对象
        }
           #region 缓冲数据
        private Bitmap btp = null;
        /// <summary>
        /// 视图浏览图片
        /// </summary>
        public Bitmap ViewBtp
        {
            get { return btp; }
        }
        // 缓冲绘画对象
        private Graphics grp = null;
        #endregion
        
        protected override void OnPaint(PaintEventArgs e)
        {
            grp.Clear(this.BackColor); // 清除背景颜色
            DrawView(); // 绘画所有图层
            e.Graphics.DrawImage(btp, new Point(0, 0)); // 重绘数据缓冲图像
        }
        /// <summary>
        /// 绘画所有
        /// </summary>
        public void DrawView()
        {
           //  在此处绘画你要重新绘画的图片
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            // 面板宽和高不全为零，重新创建缓冲对象
            if (this.Width != 0 || this.Height != 0)
            {
                btp = new Bitmap(this.Width, this.Height);
                grp = Graphics.FromImage(btp);
                base.OnSizeChanged(e);
            }
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
         // 重写空代码，解决刷新闪烁问题
        }
    }
}
